class_name Modifier
extends Node

enum Type {DMG_DEALT, DMG_TAKEN, CARD_COST, SHOP_COST, NO_MODIFIER}

@export var type: Type

func get_value(source: String) -> ModifierValue:
	for value: ModifierValue in get_children():
		if value.source == source:
			return value
	return null
	
func add_new_value(value: ModifierValue) -> void:
	var modifier_value := get_value(value.source)
	if not modifier_value:
		add_child(value)
	else:
		modifier_value.flat_value = value.flat_value
		modifier_value.percent_value = value.percent_value
		
func remove_value(source: String) -> void:
	for child: ModifierValue in get_children():
		if child.source == source:
			child.queue_free()

func clear_value() -> void:
	for child in get_children():
		child.queue_free()
		
func get_modified_value(base: int) -> int:
	var flat_base := base
	var percent_base := 1.0
	for child: ModifierValue in get_children():
		if child.type == ModifierValue.Type.FLAT:
			flat_base += child.flat_value
		elif child.type == ModifierValue.Type.PERCENT_BASED:
			percent_base += child.percent_value
	return floori(flat_base * percent_base)
